package com.hollowlatte.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hollowlatte.common.exceptionhandler.CustomException;
import com.hollowlatte.manage.entity.Course;
import com.hollowlatte.manage.entity.Score;
import com.hollowlatte.manage.entity.Student;
import com.hollowlatte.manage.mapper.CourseMapper;
import com.hollowlatte.manage.mapper.ScoreMapper;
import com.hollowlatte.manage.mapper.StudentMapper;
import com.hollowlatte.manage.service.ScoreService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Hollow Latte
 * @since 2021-05-05
 */
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {
    private final StudentMapper studentMapper;

    private final ScoreMapper scoreMapper;

    private final CourseMapper courseMapper;

    public ScoreServiceImpl(StudentMapper studentMapper, ScoreMapper scoreMapper, CourseMapper courseMapper) {
        this.studentMapper = studentMapper;
        this.scoreMapper = scoreMapper;
        this.courseMapper = courseMapper;
    }

    @Override
    public boolean saveCourseScore(Score score) {
        validateIsExistStudentAndCourse(score.getStudentId(), score.getCourseId());

        Score scoreEntity = scoreMapper.selectByStudentIdAndCourseId(score.getStudentId(), score.getCourseId());
        if (!ObjectUtils.isEmpty(scoreEntity)) {
            throw new CustomException("A0400", "此学生该课程成绩已存在");
        }

        int affect = scoreMapper.insert(score);
        return affect != 0;
    }


    @Override
    public List<Score> listScore(Long studentId) {
        Student student = studentMapper.selectById(studentId);
        if (ObjectUtils.isEmpty(student)) {
            throw new CustomException("A0400", "此学生不存在");
        }

        return scoreMapper.selectByStudentId(studentId);
    }

    @Override
    public List<Score> listScoreByTerm(Long studentId, int term) {
        Student student = studentMapper.selectById(studentId);
        List<Course> courseList = courseMapper.selectByTerm(term);
        if (ObjectUtils.isEmpty(student)) {
            throw new CustomException("A0400", "此学生不存在");
        }
        if (ObjectUtils.isEmpty(courseList)) {
            throw new CustomException("A0400", "该学期尚未有课程");
        }

        List<Score> scoreList = new ArrayList<>();
        for (Course course : courseList) {
            Score score = scoreMapper.selectByStudentIdAndCourseId(studentId, course.getId());
            scoreList.add(score);
        }

        return scoreList;
    }


    @Override
    public boolean removeCourseScore(Long studentId, Long courseId) {
        int affect = scoreMapper.deleteByStudentIdAndCourseId(studentId, courseId);

        return affect != 0;
    }


    @Override
    public boolean updateScoreByStudentIdAndCourseId(Score score) {
        validateIsExistStudentAndCourse(score.getStudentId(), score.getCourseId());

        UpdateWrapper<Score> scoreUpdateWrapper = new UpdateWrapper<>();
        scoreUpdateWrapper.eq("student_id", score.getStudentId());
        scoreUpdateWrapper.eq("course_id", score.getCourseId());

        int affect = scoreMapper.update(score, scoreUpdateWrapper);

        return affect != 0;
    }


    private void validateIsExistStudentAndCourse(Long studentId, Long courseId) {
        Student student = studentMapper.selectById(studentId);
        Course course = courseMapper.selectById(courseId);
        if (ObjectUtils.isEmpty(student)) {
            throw new CustomException("A0400", "此学生不存在");
        }
        if (ObjectUtils.isEmpty(course)) {
            throw new CustomException("A0400", "此课程不存在");
        }
    }
}
